<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./lib/bootstrap.min.css">
</head>

<body style="padding: 15px;">
    <div id="app">
        <!-- 面板区域 -->
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <!-- 添加品牌 -->
            <div class="panel-body form-inline">
                <div class="input-group">
                    <div class="input-group-addon">品牌名称</div>
                    <input type="text" class="form-control" id="exampleInputAmount" placeholder="请输入品牌名称" v-model.trim="brandName">
                </div>
                <button type="button" class="btn btn-primary" @click="addList">添加</button>
            </div>
        </div>
        <!-- 品牌列表 -->
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>#</th>
                    <th>品牌名称</th>
                    <th>状态</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{ item.id }}</td>
                    <td>{{ item.name }}</td>
                    <td>
                        <input type="checkbox" name="state" :id="item.id" v-model="item.state">
                        <label :for="item.id" v-if="item.state">已启用</label>
                        <label :for="item.id" v-else>已禁用</label>
                    </td>
                    <td>{{ item.time | dataFormat }}</td>
                    <td>
                        <a href="javascript:;" @click="del(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>


    <script src="./lib/vue.js"></script>
    <script src="./lib/dayjs.min.js"></script>
    <script>
        // Vue.filter('dataFormat', (str) => {
        //     const dt = new Date(str)

        //     const y = dt.getFullYear()
        //     const m = padZero(dt.getMonth() + 1)
        //     const d = padZero(dt.getDate())

        //     const hh = padZero(dt.getHours())
        //     const mm = padZero(dt.getMinutes())
        //     const ss = padZero(dt.getSeconds())

        //     return `${y}/${m}/${d} ${hh}:${mm}:${ss}`
        // })

        // function padZero(str) {
        //     return str > 9 ? str : '0' + str
        // }

        Vue.filter('dataFormat', (str) => {
            return dayjs(str).format('YYYY-MM-DD HH:mm:ss')
        })

        const vm = new Vue({
            el: '#app',
            data: {
                nextId: 4,
                brandName: '',
                list: [{
                    id: 1,
                    name: '宝马',
                    state: true,
                    time: new Date()
                }, {
                    id: 2,
                    name: '奔驰',
                    state: true,
                    time: new Date()
                }, {
                    id: 3,
                    name: '奥迪',
                    state: true,
                    time: new Date()
                }]
            },
            methods: {
                addList() {
                    if (this.brandName === '') return alert('内容为空！')
                    this.list.push({
                        id: this.nextId,
                        name: this.brandName,
                        state: true,
                        time: new Date()
                    })
                    this.brandName = ''
                    this.nextId++
                },
                del(id) {
                    console.log('删除')
                    this.list = this.list.filter(item => item.id != id)
                }
            }
        })
    </script>
</body>

</html>